package top.rainf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import top.rainf.constants.AttributeConstants;
import top.rainf.domain.dto.UserFullInfo;
import top.rainf.domain.vo.UserBaseInfo;
import top.rainf.domain.vo.UserPersonalInfo;
import top.rainf.service.IUserBaseInfoService;
import top.rainf.service.IUserPersonalInfoService;
import top.rainf.support.Result;
import top.rainf.support.TokenGenerator;
import top.rainf.util.StringUtil;

import java.util.List;

/**
 * @author 星环
 * @date 2023/6/1 9:32:16
 * @description
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserBaseInfoService userBaseInfoService;

    @Autowired
    private IUserPersonalInfoService userPersonalInfoService;

    @RequestMapping("/info")
    public Result info(Integer id,@RequestParam(required = false) String username){
        if(id == null && StringUtil.isNullOrEmpty(username)){
            return Result.fail("用户不存在");
        }
        if(!StringUtil.isNullOrEmpty(username)){
            UserBaseInfo baseInfo = userBaseInfoService.getByUsername(username);
            if(baseInfo == null){
                return Result.fail("用户不存在");
            }
            UserPersonalInfo userPersonalInfo = userPersonalInfoService.getOneById(baseInfo.getId());
            UserFullInfo fullInfo = new UserFullInfo(baseInfo,userPersonalInfo);
            return Result.success().addObj("user",fullInfo);
        }
        UserBaseInfo base = userBaseInfoService.getOneById(id);
        UserPersonalInfo more = userPersonalInfoService.getJoinedInfo(id);
        UserFullInfo fullInfo = new UserFullInfo(base,more);
        return Result.success().addObj("user",fullInfo);

    }

    @RequestMapping("/friendSrch")
    public Result friendSrch(@SessionAttribute(AttributeConstants.LOGIN_TOKEN) String token,
                             String username,int userId){
        int userIdFromToken = TokenGenerator.getUserIdFromToken(token);
        if(userIdFromToken != userId){
            return Result.fail("您未登录！");
        }
        List<UserBaseInfo> base = userBaseInfoService.srchByUsernameExcept(username,userId);
        return null;
    }
}
